// subPages/mine/publicWelfare/publicWelfare.js
const app = getApp()
var util =  require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    region: ['请选择', '请选择', '请选择'],
    customItem: '请选择',
    textarea: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBanner()    
    this.getIsActivity()
    this.getData()
  },

  getBanner:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/gy_banner', //接口地址
      method: 'get',
      header: {
          'content-type': 'application/json' //默认值
      },
      success: function (res) {
        if(res.data.length!=0){
          that.setData({
            isBanner: true,
            publicWelfareImageList: res.data
          })
        }
      },
      fail: function (res) {
        util.showModal('提示','读取公益海报图片失败')
      }
    })
  },

  getIsActivity:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/yes_no', //接口地址
      data: {
      },
      method: 'get',
      header: {
          'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        that.setData({
          isActivityTxt: res.data == 'yes'?true:false
        })
      },
      fail: function (res) {
        // util.showModal('提示','读取公益数据失败')
      }
    })
  },

  //获取数据
  getData:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/get_gy', //接口地址
      data: {
        userid: app.globalData.userId
      },
      method: 'get',
      header: {
          'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data){
          let region = res.data.region.split(',')
          that.setData({
            name: res.data.name,
            region: region,
            address: res.data.address,
            phone: res.data.phone,
            wechat: res.data.wechat
          })
        }
      },
      fail: function (res) {
        util.showModal('提示','读取公益数据失败')
      }
    })
  },

  //地区选择
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  //提交表单
  formSubmit:function(e){
    var that = this
    // console.log(e)
    var formId = e.detail.formId
    var name = e.detail.value.name;
    var region = e.detail.value.region;
    var address =  e.detail.value.address;
    var phone = e.detail.value.phone;
    var wechat = e.detail.value.wechat;
    if (name == "" || address == "" || phone == ""){
      util.showModal('提示','请填写完整信息')
      return
    }
    if(name.length > 0)
    {
      var type= 'name'
      var validation = util.validation(name,type)
      if(!validation){
        util.showModal('提示','姓名输入不正确')
        return
      }
    }else{
      util.showModal('提示','请输入姓名')
      return
    }
    for(var i in region){
      if(region[i] == '请选择'){
        util.showModal('提示','请选择地区')
        return 
      }
    }
    if(phone.length > 0 && phone != '')
    {
      var type= 'phone'
      var validation = util.validation(phone,type)
      if(!validation){
        util.showModal('提示','手机号输入不正确')
        return
      }
    }else{
      util.showModal('提示','请输入手机号')
      return
    }
    that.setData({
      name: name,
      region: region,
      address: address,
      phone: phone,
      wechat: wechat,
    })
    that.save(name,region,address,phone,wechat,formId)
  },

  //提交
  save:function(name,region,address,phone,wechat,formId){
    var that = this
    wx.request({
      url: app.globalData.apiUrl + '/add_gy', //本地服务器地址
      data: {
        userid: app.globalData.userId,
        form_id: formId,
        name: name,
        region: region,
        address: address,
        phone: phone,
        wechat: wechat,
        add_time: util.setTime16(new Date())
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data == '1'){
          util.showToast('保存成功','success')
          //返回首页
          wx.switchTab({
            url: '/pages/index/index',
          })
        }else{
          util.showModal('提示','保存失败，请重新保存')
          return
        }
      },
      fail: function (res) {
        util.showModal('提示','保存失败，请重新保存')
        return
      }
    })
  },

  activity:function(){
    this.setData({
      isActivity: true,
      textarea: false
    })
  },

  //关闭
  close:function(){
    this.setData({
      isActivity: false,
      textarea: true
    })
  },

  //阻止穿透
  preventTouchMove: function () {
    // console.log('stop user scroll it!');
    return;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})